#include "MenuScene.h"
#include "AboutScene.h"
#include "GameScene.h"

CCScene* Menu::scene() {

	CCScene *scene = CCScene::create();
	Menu *layer = Menu::create();
	scene->addChild(layer);
	return scene;
}

bool Menu::init() {
	if (!CCLayer::init()) {
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCMenuItemImage *pAboutItem = CCMenuItemImage::create("about.png",
			"about.png", this, menu_selector(Menu::menuAboutCallback));
	pAboutItem->setPosition(ccp(visibleSize.width - 120, 40));
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("exit.png",
			"exit.png", this, menu_selector(Menu::menuCloseCallback));
	pCloseItem->setPosition(ccp(visibleSize.width - 40, 40));
	CCMenuItemImage *pStartItem = CCMenuItemImage::create("start.png",
			"start.png", this, menu_selector(Menu::menuStartCallback));
	pStartItem->setPosition(
			ccp(visibleSize.width / 2, visibleSize.height / 2 - 100));

	CCMenu* pMenu = CCMenu::create(pStartItem, pAboutItem, pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	CCSprite* pBackgroundSprite = CCSprite::create("bg.png");

	pBackgroundSprite->setPosition(
			ccp(visibleSize.width/2, visibleSize.height/2));

	this->addChild(pBackgroundSprite, 0);

	return true;
}

void Menu::menuCloseCallback(CCObject* pSender) {
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

void Menu::menuAboutCallback(CCObject* pSender) {
	CCScene *pAboutScene = About::scene();
	CCDirector::sharedDirector()->setDepthTest(true);
	CCDirector::sharedDirector()->replaceScene(
			CCTransitionPageTurn::create(1, pAboutScene, false));
}

void Menu::menuStartCallback(CCObject* pSender) {
	CCScene *pGameScene = Game::scene();
	CCDirector::sharedDirector()->setDepthTest(true);
	CCDirector::sharedDirector()->replaceScene(
			CCTransitionFade::create(1, pGameScene, ccBLACK));
}
